const { insertMenu, getOneMenu, update, getMenuInfo, getMenuWhere, deleteMenu } = require('../model/menu.model');

class MenuService {
    // 插入菜单
    async createMenu({ pid, name, path, icon, order, key }) {
        if (!pid) {
          pid = 0
        }

        const params = { pid, name, path, icon, order, key }
        const res = await insertMenu('b_menu', params);
        return res;
    }
    // 修改菜单
    async updateMenu({ id, name, path, icon, order }) {
        const params = {}
        const whereOpt = {}

        id && Object.assign(whereOpt, { id })

        name && Object.assign(params, { name })
        path && Object.assign(params, { path })
        icon && Object.assign(params, { icon })
        order && Object.assign(params, { order })

        const res = await update('b_menu', params, whereOpt);
        return res;
    }
    // 删除菜单
    async deleteMenu({ id }) {
        const params = {}

        id && Object.assign(params, { id })

        const res = await deleteMenu('b_menu', params);
        return res;
    }

    // 查询单个菜单信息
    async getMenuBy({id,name}) {
      const whereOpt = {}

      id && Object.assign(whereOpt, { id })
      name && Object.assign(whereOpt, { name })

      const res = await getOneMenu('b_menu', whereOpt)

      return res.length > 0 ? res : null;
     
    }

    // 查询左侧菜单
    async getLeftMenu() {
      const sql = `select * from b_menu`;
      const res = await getMenuInfo(sql);
      return res
    }

    // 查询菜单树
    async getMenuList() {
      const sql = `select * from b_menu`;
      const res = await getMenuWhere(sql)

      return res.length > 0 ? res : null;
      
    }
}

module.exports = new MenuService()